package com.pwp.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Display;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.agent.crm.base.BaseActivity;
import com.agent.crm.config.AppConfig;
import com.agent.crm.view.MainActivity;
import com.agent.crm.view.NoteBookActivity;
import com.agent.crm.view.R;
import com.agent.crm.view.SearchActivity;
import com.pwp.borderText.BorderText;
import com.pwp.dao.ScheduleDAO;
import com.pwp.activity.ScheduleInfoView;

/**
 * 日历显示activity
 * @author jack_peng
 *
 */
public class CalendarActivity extends BaseActivity implements OnGestureListener {

	private ViewFlipper flipper = null;
	private GestureDetector gestureDetector = null;
	private CalendarView calV = null;
	private GridView gridView = null;
	private BorderText topText = null;
	private Drawable draw = null;
	private static int jumpMonth = 0;      //每次滑动，增加或减去一个月,默认为0（即显示当前月）
	private static int jumpYear = 0;       //滑动跨越一年，则增加或者减去一年,默认为0(即当前年)
	private int year_c = 0;
	private int month_c = 0;
	private int day_c = 0;
	private String currentDate = "";
	private View clickDate=null;
	
	private ScheduleDAO dao = null;
	

	public CalendarActivity() {

		Date date = new Date();
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");
    	currentDate = sdf.format(date);  //当期日期
    	year_c = Integer.parseInt(currentDate.split("-")[0]);
    	month_c = Integer.parseInt(currentDate.split("-")[1]);
    	day_c = Integer.parseInt(currentDate.split("-")[2]);
    	
    	dao = new ScheduleDAO(this);
    	
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		int gvFlag = 0;         //每次添加gridview到viewflipper中时给的标记
		if (e1.getX() - e2.getX() > 120) {
            //像左滑动
			addGridView();   //添加一个gridView
			jumpMonth++;     //下一个月
			
			calV = new CalendarView(this, getResources(),jumpMonth,jumpYear,year_c,month_c,day_c);
	        gridView.setAdapter(calV);
	        //flipper.addView(gridView);
	        addTextToTopTextView(topText);
	        gvFlag++;
	        flipper.addView(gridView, gvFlag);
			this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.push_left_in));
			this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.push_left_out));
			this.flipper.showNext();
			flipper.removeViewAt(0);
			return true;
		} else if (e1.getX() - e2.getX() < -120) {
            //向右滑动
			addGridView();   //添加一个gridView
			jumpMonth--;     //上一个月
			
			calV = new CalendarView(this, getResources(),jumpMonth,jumpYear,year_c,month_c,day_c);
	        gridView.setAdapter(calV);
	        gvFlag++;
	        addTextToTopTextView(topText);
	        //flipper.addView(gridView);
	        flipper.addView(gridView,gvFlag);
	        
			this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.push_right_in));
			this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.push_right_out));
			this.flipper.showPrevious();
			flipper.removeViewAt(0);
			return true;
		}
		return false;
	}
	
	/**
	 * 创建菜单
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

//		menu.add(0, menu.FIRST, menu.FIRST, "今天");
//		menu.add(0, menu.FIRST+1, menu.FIRST+1, "跳转");
//		menu.add(0, menu.FIRST+2, menu.FIRST+2, "日程");
//		menu.add(0, menu.FIRST+3, menu.FIRST+3, "日期转换");
		menu.add(0, menu.FIRST, menu.FIRST, "当天日程");
		menu.add(0, menu.FIRST+1, menu.FIRST+1, "本周日程");
		return super.onCreateOptionsMenu(menu);
	}
	
	/**
	 * 选择菜单
	 */
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		SharedPreferences preferences=getSharedPreferences("scheduleStatusMode", Context.MODE_PRIVATE);
		switch (item.getItemId()){
        case Menu.FIRST:
        	//跳转到今天
        	Editor eDay=preferences.edit();
	        eDay.putString("CalendarDefaultLunchMode", "DayCalendar");
	        eDay.commit();
	        Intent intent = new Intent();
	        Date date = new Date();
	    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");
	    	String currentDate = sdf.format(date);  //当期日期
	    	String year_c = currentDate.split("-")[0];
	    	String month_c = currentDate.split("-")[1];
	    	String day_c = currentDate.split("-")[2];
	    	Calendar car=Calendar.getInstance();
	    	car.setTime(new Date());
	    	ArrayList<String> scheduleDate = makeScheduleDate(year_c, month_c, day_c,car.get(Calendar.DAY_OF_WEEK)-1);
            intent.putStringArrayListExtra("scheduleDate", scheduleDate);
			intent.setClass(this, ScheduleInfoView.class);
			startActivity(intent);
			finish();
        	break;
        case Menu.FIRST+1:
        	Editor eWeek=preferences.edit();
	        eWeek.putString("CalendarDefaultLunchMode", "WeekCalendar");
	        eWeek.commit();
	    	Intent intentWeek = new Intent();
	    	intentWeek.setClass(this, CalendarWeekActivity.class);
	        startActivity(intentWeek);
	        finish();
        }
		return super.onMenuItemSelected(featureId, item);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {

		return this.gestureDetector.onTouchEvent(event);
	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}
	
	//添加头部的年份 闰哪月等信息
	public void addTextToTopTextView(TextView view){
		StringBuffer textDate = new StringBuffer();
//		draw = getResources().getDrawable(R.drawable.top_day);
		draw = getResources().getDrawable(R.drawable.head);
		view.setBackgroundDrawable(draw);
		textDate.append(calV.getShowYear()).append("年").append(
				calV.getShowMonth()).append("月").append("\t");
		if (!calV.getLeapMonth().equals("") && calV.getLeapMonth() != null) {
			textDate.append("闰").append(calV.getLeapMonth()).append("月")
					.append("\t");
		}
		textDate.append(calV.getAnimalsYear()).append("年").append("(").append(
				calV.getCyclical()).append("年)");
		view.setText(textDate);
		view.setTextColor(Color.BLACK);
		view.setTypeface(Typeface.DEFAULT_BOLD);
	}
	
	//添加gridview
	private void addGridView() {
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		//取得屏幕的宽度和高度
		WindowManager windowManager = getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        int Width = display.getWidth(); 
        int Height = display.getHeight();
        
		gridView = new GridView(this);
		gridView.setNumColumns(7);
		gridView.setColumnWidth(46);
		
		if(Width == 480 && Height == 800){
			gridView.setColumnWidth(69);
		}
		gridView.setGravity(Gravity.CENTER_VERTICAL);
		gridView.setSelector(new ColorDrawable(Color.TRANSPARENT)); // 去除gridView边框
		gridView.setVerticalSpacing(1);
		gridView.setHorizontalSpacing(1);
        gridView.setBackgroundResource(R.drawable.gridview_bk);
		gridView.setOnTouchListener(new OnTouchListener() {
            //将gridview中的触摸事件回传给gestureDetector
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				return CalendarActivity.this.gestureDetector
						.onTouchEvent(event);
			}
		});

		
		gridView.setOnItemClickListener(new OnItemClickListener() {
            //gridView中的每一个item的点击事件
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				  //点击任何一个item，得到这个item的日期(排除点击的是周日到周六(点击不响应))
				  clickDate=arg1;
				  int startPosition = calV.getStartPositon();
				  int endPosition = calV.getEndPosition();
				  if(startPosition <= position  && position <= endPosition){
					  String scheduleDay = calV.getDateByClickItem(position).split("\\.")[0];  //这一天的阳历
					  //String scheduleLunarDay = calV.getDateByClickItem(position).split("\\.")[1];  //这一天的阴历
	                  String scheduleYear = calV.getShowYear();
	                  String scheduleMonth = calV.getShowMonth();
	                  String week = "";
	                  
	                  //通过日期查询这一天是否被标记，如果标记了日程就查询出这天的所有日程信息
	                  String[] scheduleIDs = dao.getScheduleByTagDate(Integer.parseInt(scheduleYear), Integer.parseInt(scheduleMonth), Integer.parseInt(scheduleDay),3);
	                  ArrayList<String> scheduleDate = makeScheduleDate(scheduleYear, scheduleMonth, scheduleDay, position);
	                  if(scheduleIDs != null && scheduleIDs.length > 0){
	                	  //跳转到显示这一天的所有日程信息界面
		  				  Intent intent = new Intent();
		  				  intent.setClass(CalendarActivity.this, ScheduleInfoView.class);
		                  intent.putExtra("scheduleID", scheduleIDs);
		                  intent.putStringArrayListExtra("scheduleDate", scheduleDate);
		  				  startActivity(intent);  
	                  }else{
	                  //直接跳转到需要添加日程的界面
	                	  
		                  Intent intent = new Intent();
		                  intent.putExtra("operateType", "add");
		                  intent.putStringArrayListExtra("scheduleDate", scheduleDate);
		                  intent.setClass(CalendarActivity.this, ScheduleView.class);
		                  startActivity(intent);
	                  }
				  }
			}
		});
		gridView.setLayoutParams(params);


	}
	
	
	public ArrayList<String> makeScheduleDate(String scheduleYear,String scheduleMonth,String scheduleDay,int position){
		String week = "";
		//得到这一天是星期几
        switch(position%7){
        case 0:
      	  week = "星期日";
      	  break;
        case 1:
      	  week = "星期一";
      	  break;
        case 2:
      	  week = "星期二";
      	  break;
        case 3:
      	  week = "星期三";
      	  break;
        case 4:
      	  week = "星期四";
      	  break;
        case 5:
      	  week = "星期五";
      	  break;
        case 6:
      	  week = "星期六";
      	  break;
        }
		 
        ArrayList<String> scheduleDate = new ArrayList<String>();
        scheduleDate.add(scheduleYear);
        scheduleDate.add(scheduleMonth);
        scheduleDate.add(scheduleDay);
        scheduleDate.add(week);
        //scheduleDate.add(scheduleLunarDay);
        return scheduleDate;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected int initLayout() {
		// TODO Auto-generated method stub
		return R.layout.main_calendar;
	}

	@Override
	protected void initViewCmp() {
		// TODO Auto-generated method stub
		Button btn_Contact =(Button)findViewById(R.id.contact);
		Button btn_carlendar=(Button)findViewById(R.id.schedule_and_carlendar);
		btn_carlendar.setBackgroundResource(R.drawable.home_btn_bg_d);
		Button btn_search=(Button)findViewById(R.id.search_contact_schedule);
		Button note_book=(Button)findViewById(R.id.note_book);
		Button btn_attention = (Button)findViewById(R.id.attention_contact);
		btn_Contact.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent_main = new Intent();
				intent_main.setClass(CalendarActivity.this, MainActivity.class);
	            startActivity(intent_main);
			}
		});
		btn_search.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent_search = new Intent();
	            intent_search.setClass(CalendarActivity.this, SearchActivity.class);
	            startActivity(intent_search);
			}
		});
		note_book.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent noteIntent = new Intent();
	            noteIntent.setClass(CalendarActivity.this, NoteBookActivity.class);
	            startActivity(noteIntent);
			}
		});
		btn_attention.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				 Intent aatentionIntent = new Intent();
		            aatentionIntent.setClass(CalendarActivity.this, ContactAttentionActivity.class);
		            startActivity(aatentionIntent);
			}
		});
		gestureDetector = new GestureDetector(this);
		flipper = (ViewFlipper) findViewById(R.id.flipper);
		flipper.removeAllViews();
		calV = new CalendarView(this, getResources(), jumpMonth, jumpYear,
				year_c, month_c, day_c);

		addGridView();
		gridView.setAdapter(calV);
		// flipper.addView(gridView);
		flipper.addView(gridView, 0);

		topText = (BorderText) findViewById(R.id.toptext);
		addTextToTopTextView(topText);
	}

	@Override
	protected void initListener() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
//		showLoadingDialog("请稍后");
	}

	@Override
	protected void initAsycnData() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected View initCustomLayout() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void changeDefaultSetting() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if(AppConfig.backPress==1){
			//跳转到今天
			if(null!=clickDate){
				clickDate.setBackgroundResource(R.drawable.mark);
				clickDate.postInvalidate();
				clickDate=null;
				AppConfig.backPress=0;
			}
		}
		if(AppConfig.backPress==2){
			//跳转到今天
			if(null!=clickDate){
				clickDate.setBackgroundResource(R.drawable.empty_day_bgc);
				clickDate.postInvalidate();
				clickDate=null;
				AppConfig.backPress=0;
			}
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		dao.destoryDB();
	}
	
}